package com.tensquare.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Channel;
import com.tensquare.article.service.ArticleService;
import com.tensquare.article.service.ChannelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Retention;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/channel")
@CrossOrigin
public class ChannelController {
    @Autowired
    private ChannelService channelService;
    //POST/channel增加频道
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Channel channel){
        channelService.save(channel);
        return new Result(true, StatusCode.OK,"baocun ok");
    }
    //GET/channel 频道全部列表
    @GetMapping
    public Result findAll(){
        List<Channel> list=channelService.findAll();
        return  new Result(true,StatusCode.OK,"ok",list);
    }
    //GET/channel/{channelId}根据ID查询频道
    @GetMapping("{channelId}")
    public  Result findByid(@PathVariable String channelId ){
        Channel channel= channelService.findById(channelId);
        return  new Result(true,StatusCode.OK,"ok",channel);
    }
    //PUT/channel/{channelId}修改频道
    @PutMapping("{channelId}")
    public  Result updateByid(@PathVariable String channelId,@RequestBody Channel channel ){
        channel.setId(channelId);
        channelService.updateById(channel);
        System.out.println(channel);
        return  new Result(true,StatusCode.OK,"ok");
    }

   // DELETE/channel/{channelId} 根据ID删除频道
    @DeleteMapping("{channelId}")
   public  Result deleteByid(@PathVariable String channelId){
        Channel channel=new Channel();
        channel.setId(channelId);
        channelService.deleteById(channel);
        return new Result(true,StatusCode.OK,"ojbk");
   }

    //POST/channel/search{page}/{size}/频道分页
    @PostMapping("/search/{page}/{size}")
    public  Result findByPage(@PathVariable Integer page,
                                @PathVariable Integer size,
                                @RequestBody Map<String,Object> map){
        Page<Channel> pageData=channelService.findByPage(map,page,size);
        PageResult<Channel> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(true,StatusCode.OK,"ok",pageResult);

    }
    }
